package logic

import (
	"context"
	"fmt"
	"houses_srv/cache"
	"houses_srv/pkg"
	"math/rand"
	"strconv"
	"time"

	"houses_srv/houses"
	"houses_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *houses.SendSmsRequest) (*houses.SendSmsResponse, error) {
	// todo: add your logic here and delete this line

	code := rand.Intn(9000) + 1000
	sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	fmt.Println(sms, err)
	/*if err != nil {
		return nil, err
	}
	if *sms.Body.Message != "OK" {
		return nil, errors.New(*sms.Body.Message)
	}*/

	cache.Rdb.Set(l.ctx, "sendSms"+in.Source+in.Mobile, code, time.Minute*5)
	return &houses.SendSmsResponse{}, nil
}
